package com.rrd.dw.mr.gzip.kafka;

import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.kafka.CrawlResult;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.loan.PhoneBook;
import com.ucredit.babel.common.model.loan.PhoneBookRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 通话记录数据
 *
 * @author renzhanzheng
 */

public class NewPhoneBookParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     *  通话记录数据
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        PhoneBookRecord obj = (PhoneBookRecord) model;
        List<PhoneBook> list = obj.getPhoneBooks() == null ? new ArrayList<>() : obj.getPhoneBooks();

        StringBuilder sb = getBaseInfo(obj, successful);
        String timestamstr = obj.getTimestamp() == null ? "" : DateUtil.toDateStrMs(new Date(obj.getTimestamp()));

        setFileNum(20);
        StringBuilder detailSb = new StringBuilder(500);
        int i = 0;
        for (PhoneBook c :list){
            i++;
            detailSb.delete(0, detailSb.length());
            //序号
            detailSb.append(i).append(CRT001);
            //通讯录人名
            detailSb.append(c.getName() == null ? "" : c.getName()).append(CRT001);
            //通讯录号码
            detailSb.append(c.getMobile() == null ? "" : c.getMobile()).append(CRT001);
            //公司名称(对应hbase remark)
            detailSb.append(c.getRemark() == null ? "" : c.getRemark()).append(CRT001);
            //创建时间
            detailSb.append(c.getCreateTime() == null ? "" : c.getCreateTime()).append(CRT001);
            //最近被联系时间
            detailSb.append(c.getCalledTime() ==null ? "" : c.getCalledTime()).append(CRT001);
            //修改时间
            detailSb.append(c.getModifyTime() == null ? "" : c.getModifyTime()).append(CRT001);
            //callCount
            detailSb.append(c.getCallCount() == null ? "" : c.getCallCount()).append(CRT001);
            //邮件地址
            detailSb.append(c.getEmail() == null ? "" : c.getEmail()).append(CRT001);
            //职位
            detailSb.append(c.getTitle() == null ? "" : c.getTitle()).append(CRT001);
            //系统类型
            detailSb.append(c.getPhoneSystem() == null ? "" : c.getPhoneSystem()).append(CRT001);
            //手机类型
            detailSb.append(c.getPhoneType() == null ? "" : c.getPhoneType()).append(CRT001);
            //campany
            detailSb.append(c.getCompany() == null ? "" : c.getCompany()).append(CRT001);
            //通话次数
            detailSb.append(c.getPosition() == null ? "" : c.getPosition()).append(CRT001);
            //address
            detailSb.append(c.getAddress() == null ? "" : c.getAddress()).append(CRT001);
            detailSb.append(timestamstr).append(CRT001);

            key.set(getKey());
            val.set((sb.toString() + detailSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        if (i == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }


    @Override
    public String getTableName() {
        return "kafka_babel_phone_book_info_inc";
    }

    @Override
    public String getType() {
        return RecordType.PHONE_BOOK.toString();
    }

    @Override
    public String getComment() {
        return "通话记录数据";
    }
}
